﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IFTRUE.EIMS.Web
{
    /// <summary>
    /// 作用：页面基类
    /// 作者：iftrue
    /// 日期：2013-08-19
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        /// <summary>
        /// 重写OnPreInit,进行页面级权限验证
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            //首先判断请求是否来自同一个域
            try
            {
                if (Request.ServerVariables["HTTP_Referer"] == null || Request.ServerVariables["REMOTE_HOST"] == null || Request.ServerVariables["SERVER_NAME"] != Request.Url.Host)
                {
                    Model.Log logModel = new Model.Log();
                    logModel.Content = "跨域请求(" + Request.Url.ToString() + ")";
                    logModel.CreateDate = DateTime.Now;
                    logModel.IP = Helper.IpHelper.GetClientIP();
                    logModel.Title = "域名检查";
                    logModel.Type = 3;
                    new BLL.Log().Add(logModel);
                    Response.Redirect(this.ResolveUrl("~/Login.aspx"), true);
                }
            }
            catch (Exception ex)
            {
                Model.Log logModel=new Model.Log();
                logModel.Content="域名检查异常("+Request.Url.ToString()+")("+ex.Message+")";
                logModel.CreateDate=DateTime.Now;
                logModel.IP=Helper.IpHelper.GetClientIP();
                logModel.Title="域名检查";
                logModel.Type=4;
                new BLL.Log().Add(logModel);
                Response.Redirect(this.ResolveUrl("~/Login.aspx"), true);
            }
            //如果Session不存在则跳转到登录页
            if (MySession.CurrentUser == null)
            {
                Response.Redirect(this.ResolveUrl("~/Login.aspx"), true);
            }
        }
    }
}